package com.yzh.openai.trigger.job;


import com.yzh.openai.domain.order.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 超时关闭订单任务
 */
@Slf4j
@Component
public class TimeoutCloseOrderJob {
    @Resource
    private IOrderService orderService;
    @Value("${lantu.wxpay.config.mchid}")
    private String mchid;

    /**
     * 每1分钟扫描超时关单任务
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void exec() {
        try {
            List<String> timeOutList = orderService.queryTimeoutCloseOrderList();
            if (timeOutList.isEmpty()) {
                log.info("定时任务【超时关单】无超时订单");
                return;
            }
            for (String orderId : timeOutList) {
                //关单
                boolean success = orderService.changeOrderClose(orderId);
                log.info("定时任务【超时关单】关闭结果：{} 订单：{}", success, orderId);
            }
        } catch (Exception e) {
            log.error("定时任务【超时关单】异常", e);
        }
    }
}
